module TranslitTo

  def self.included(base)
    base.extend(ClassMethods)
  end

  class Config
    attr_reader :model
    attr_reader :model_id

    def initialize(model_id)
      @model_id = model_id
      @model = model_id.to_s.camelize.constantize
    end

    def model_name
      @model_id.to_s
    end
  end

  module ClassMethods
	  def translit_to(model_id = nil)
      # converts Foo::BarController to 'bar' and FooBarsController to 'foo_bar'
      # and AddressController to 'address'
      model_id = self.to_s.split('::').last.sub(/Controller$/, '').pluralize.singularize.underscore unless model_id
      @translit_to_config = TranslitTo::Config.new(model_id)
    end

    # Make the @translit_to_config class variable easily
    # accessable from the instance methods.
    def translit_to_config
      @translit_to_config || self.superclass.instance_variable_get('@translit_to_config')
    end
  end

end